<script setup>
import { Buffer } from 'buffer';
const MicrobitUUIDS = {
  LEDService: 'e95dd91d-251d-470a-a062-fa1922dfa9a8',
  LEDText: 'e95d93ee-251d-470a-a062-fa1922dfa9a8',
  TemperatureService: 'e95d6100-251d-470a-a062-fa1922dfa9a8',
  TemperatureData: 'e95d9250-251d-470a-a062-fa1922dfa9a8',
  PINService: 'e95d127b-251d-470a-a062-fa1922dfa9a8',
  PinData: 'e95d8d00-251d-470a-a062-fa1922dfa9a8',
  PinADConfiguration: 'e95d5899-251d-470a-a062-fa1922dfa9a8',
  PinIOConfiguration: 'e95db9fe-251d-470a-a062-fa1922dfa9a8',
  PinPWMControl: 'e95dd822-251d-470a-a062-fa1922dfa9a8',
  ButtonService: 'E95D9882-251D-470A-A062-FA1922DFA9A8'.toLowerCase(),
  ButtonAState: 'E95DDA90-251D-470A-A062-FA1922DFA9A8'.toLowerCase(),
  ButtonBState: 'E95DDA91-251D-470A-A062-FA1922DFA9A8'.toLowerCase(),
};

const options = {
  filters: [
    { name: 'BBC micro:bit' },
    { namePrefix: 'BBC micro:bit' }
  ],
  optionalServices: [MicrobitUUIDS.LEDService, MicrobitUUIDS.PINService, MicrobitUUIDS.TemperatureService, MicrobitUUIDS.ButtonService]
}

let server;
let device;

async function connect() {
   device = await navigator.bluetooth.requestDevice(options);
   server = await device.gatt.connect();
   console.log(server, device.gatt.connected);

   // 监听蓝牙消息
}

async function sendText() {
  try {
    const ledService = await server.getPrimaryService(MicrobitUUIDS.LEDService);
    const ledTextCharacteristic = await ledService.getCharacteristic(MicrobitUUIDS.LEDText);
    const text = 'sjjshshs!';
    const encoder = new TextEncoder('utf-8');
    const textArrayBuffer = encoder.encode(text);
    await ledTextCharacteristic.writeValue(textArrayBuffer);
    console.log('文字已发送到LED显示屏:', text);
  } catch (e) {
    console.log(e);
  }
}

async function initPinEvent() {
  const pinService = await server.getPrimaryService(MicrobitUUIDS.PINService);
  const pinDataCharacteristic = await pinService.getCharacteristic(MicrobitUUIDS.PinData);

  await pinDataCharacteristic.startNotifications();

  pinDataCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
    console.log(event);
  })

  console.log('添加引脚事件成功!');
}

async function write0Analoge() {
  const pinService = await server.getPrimaryService(MicrobitUUIDS.PINService);
  // const pinDataCharacteristic = await pinService.getCharacteristic(MicrobitUUIDS.PinData);
  const pinIOConfigurationCharacteristic = await pinService.getCharacteristic(MicrobitUUIDS.PinIOConfiguration);
  // const pinAdConfigurationCharacteristic = await pinService.getCharacteristic(MicrobitUUIDS.PinADConfiguration);


  try {
    // 设置pin IO模式为写入
    const pinDataValue = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
    await pinIOConfigurationCharacteristic.writeValue(pinDataValue);
    // // 设置pin 信号模式为数字或者模拟
    // const pinAdConfigurationValue = new Uint8Array([0x01, 0x00]);
    // await pinAdConfigurationCharacteristic.writeValue(pinAdConfigurationValue);

    // // 写入数据
    // const pinDataValue1 = new Uint8Array([0x01, 0x01, 0x00, 0x00]);
    // await pinDataCharacteristic.writeValue(pinDataValue1);
    console.log('写入成功！');
  } catch (e) {
    console.log(e);
  }
}

async function listenButtonEvent() {
  const buttonService = await server.getPrimaryService(MicrobitUUIDS.ButtonService);
  const ButtonACharacteristic = await buttonService.getCharacteristic(MicrobitUUIDS.ButtonAState);
  const ButtonBCharacteristic = await buttonService.getCharacteristic(MicrobitUUIDS.ButtonBState);


  await ButtonACharacteristic.startNotifications();
  await ButtonBCharacteristic.startNotifications();

  ButtonACharacteristic.addEventListener('characteristicvaluechanged', (event) => {
    console.log(event);
  })

  ButtonBCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
    console.log(event);
  })

  console.log('添加按钮监听事件成功');
}
</script>

<template>
  <div>
    <button @click="connect">microbit connect</button>

    <button @click="sendText">发送文字</button>

    <button @click="listenButtonEvent">监听按钮事件</button>

    <button @click="initPinEvent">初始化引脚事件</button>

    <button @click="write0Analoge">引脚P0写入模拟数据1</button>
  </div>
</template>

<style scoped>

</style>
